package com.frcdi.qms.web.controller.equipment;

import com.frcdi.qms.common.core.controller.BaseController;
import com.frcdi.qms.common.core.domain.AjaxResult;
import com.frcdi.qms.common.core.page.TableDataInfo;
import com.frcdi.qms.common.utils.poi.ExcelUtil;
import com.frcdi.qms.equipment.domain.EquipmentCommunication;
import com.frcdi.qms.equipment.service.IEquipmentCommunicationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备通讯配置Controller
 *
 * @author cell
 * @date 2023-12-26
 */
@Api(tags = "设备通讯配置")
@RestController
@RequestMapping("/equipment/communication")
public class EquipmentCommunicationController extends BaseController
{
    @Autowired
    private IEquipmentCommunicationService equipmentCommunicationService;

    /**
     * 查询设备通讯配置列表
     */
    @ApiOperation("查询设备通讯配置列表")
    @PreAuthorize("@ss.hasPermi('equipment:equipmentBase:edit')")
    @GetMapping("/list")
    public TableDataInfo list(EquipmentCommunication equipmentCommunication)
    {
        startPage();
        List<EquipmentCommunication> list = equipmentCommunicationService.selectEquipmentCommunicationList(equipmentCommunication);
        return getDataTable(list);
    }

    /**
     * 导出设备通讯配置列表
     */
    @ApiOperation("导出设备通讯配置列表")
    @PreAuthorize("@ss.hasPermi('equipment:equipmentBase:edit')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, EquipmentCommunication equipmentCommunication)
    {
        List<EquipmentCommunication> list = equipmentCommunicationService.selectEquipmentCommunicationList(equipmentCommunication);
        ExcelUtil<EquipmentCommunication> util = new ExcelUtil<EquipmentCommunication>(EquipmentCommunication.class);
        util.exportExcel(response, list, "设备通讯配置数据");
    }

    /**
     * 获取设备通讯配置详细信息
     */
    @ApiOperation("获取设备通讯配置详细信息")
    @PreAuthorize("@ss.hasPermi('equipment:equipmentBase:edit')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(equipmentCommunicationService.selectEquipmentCommunicationById(id));
    }

    /**
     * 新增设备通讯配置
     */
    @ApiOperation("新增设备通讯配置")
    @PreAuthorize("@ss.hasPermi('equipment:equipmentBase:edit')")
    @PostMapping
    public AjaxResult add(@RequestBody EquipmentCommunication equipmentCommunication)
    {
        if (!equipmentCommunicationService.checkequipmentUnique(equipmentCommunication)){
            return error("新增失败，设备已存在");
        }
        equipmentCommunication.setCreateBy(getUsername());
        return toAjax(equipmentCommunicationService.insertEquipmentCommunication(equipmentCommunication));
    }

    /**
     * 修改设备通讯配置
     */
    @ApiOperation("修改设备通讯配置")
    @PreAuthorize("@ss.hasPermi('equipment:equipmentBase:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody EquipmentCommunication equipmentCommunication)
    {
        if (!equipmentCommunicationService.checkequipmentUnique(equipmentCommunication)){
        return error("修改失败，已存在");
        }
        equipmentCommunication.setUpdateBy(getUsername());
        return toAjax(equipmentCommunicationService.updateEquipmentCommunication(equipmentCommunication));
    }

    /**
     * 删除设备通讯配置
     */
    @ApiOperation("删除设备通讯配置")
    @PreAuthorize("@ss.hasPermi('equipment:equipmentBase:edit')")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(equipmentCommunicationService.deleteEquipmentCommunicationByIds(ids));
    }
}
